SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [paEjecutarAperturaDeReestructura]

@reestructura INT,
@op INT,
@accion INT,
@id INT

AS

DECLARE @Estado INT

SELECT @Estado = (SELECT Estado FROM tb_Reestructuras WHERE IdReestructura = @reestructura)

IF @Estado NOT IN(15, 16, 18) BEGIN
	-- OP = 1 Es Plazas
	IF @op = 1 BEGIN
		--ACCION = 2 Es Baja
		IF @accion = 2 BEGIN
			DELETE tb_AperturasDeReestructura WHERE IdAperturaReestructura = @id
		END
	END
	-- OP = 2 Es Divisiones
	IF @op = 2 BEGIN
		--ACCION = 2 Es Baja
		--DEBO BORRAR LAS PLAZAS RELACIONADAS A ABRIR
		IF @accion = 2 BEGIN
			DELETE tb_AperturasDeReestructura WHERE DivisionAAbrir = @id
		DELETE tb_DivisionesAAbrir WHERE IdDivision = @id
		END
	END
END




GO
GRANT EXECUTE ON  [dbo].[paEjecutarAperturaDeReestructura] TO [Reestructura]
GO
